<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\OrderCard;
use app\model\Order;
use app\model\GoodsGiftcardType;

class OrdercardController extends Base
{
    public function goodsgiftcard()
    {
        $sid = input('get.sid', '', 'serach_in');
        $goods_id = input('get.goodsId', '', 'serach_in');
        $price = input('get.price', '', 'serach_in');
        $gcdata = [];
        $weid = weid();
        $where['weid'] =  $weid;
        $where['uid'] =   UID();
        $where['sid'] =   $sid;
        $where['ptype'] =  3;
        $Giftcardids = GoodsGiftcardType::getidsbygoods($goods_id);
        $data = [];

        if (!empty($Giftcardids)) {
            $query = OrderCard::where($where);
            //$query->where('end_time', '>=', time());
            $query->where('card_tid', 'in', $Giftcardids);

            $gcdata =  $query->select()->toArray();

            //$sql =  $query->getLastsql();
        }
        if (!empty($gcdata)) {
            $i = 0;
            foreach ($gcdata as &$vo) {

                if (Order::where('id', $vo['order_id'])->where('pay_time', '>', 0)->find()) {

                    $vo['minialias'] = substr($vo['order_num_alias'], -5);
                    $vo['styleno'] = substr($vo['id'], -1);
                    if ($vo['styleno'] > 5) {
                        $vo['styleno'] = $vo['styleno'] - 5;
                    }

                    $data[$i] = $vo;
                    $i++;
                }
            }
        }
        return $this->json(['data' => $data, 'sql' => '',]);
    }
}
